package com.gsafety.libraryktx.util

import android.content.Context
import android.graphics.Color
import android.graphics.Matrix
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
import com.gsafety.libraryktx.R
import com.gsafety.libraryktx.bean.AxisText
import com.gsafety.libraryktx.widget.LineChartMarkView

/**折线管理
 * @author : lj
 * @date : 2020/7/31 14:18
 */
class LineChartManager(lineChart: LineChart) {
    var mlineChart: LineChart? = null
    var leftAxis: YAxis? = null
    var rightAxis: YAxis? = null
    var xAxis: XAxis? = null

    init {
        mlineChart = lineChart
        leftAxis = lineChart.axisLeft
        rightAxis = lineChart.axisRight
        xAxis = lineChart.xAxis
    }
    private fun initChart(context: Context) {
        mlineChart?.let {
            it.setScaleEnabled(false)
            it.setBackgroundColor(context.resources.getColor(R.color.color_white))
            it.setDrawGridBackground(false)
            //不显示边界
            it.setDrawBorders(false)
            it.description.isEnabled = false
            it.legend.isEnabled = false
            //XY轴的设置
            xAxis!!.position = XAxis.XAxisPosition.BOTTOM//X轴设置显示位置在底部
            xAxis!!.granularity = 1f//设置最小间隔，防止当放大时，出现重复标签
            xAxis!!.textColor = Color.GRAY//设置X轴字体颜色
            xAxis!!.textSize = 10f //设置X轴字体大小
            xAxis!!.setDrawGridLines(false) //不绘制X轴网格，默认为绘制。
            //y轴设置
            leftAxis!!.axisMinimum = 0f //保证Y轴从0开始，不然会上移一点
            leftAxis!!.setDrawGridLines(false)

            rightAxis!!.enableGridDashedLine(10f,10f,0f)
            it.marker= LineChartMarkView(context)
            //将条目放大 可滑动
            it.invalidate()
            var mMatrix = Matrix();
            mMatrix.postScale(1f, 1f); //X轴宽度放大2倍 竖直方向不变
            it.viewPortHandler.refresh(mMatrix, it, false)
        }
    }

    /**初始化曲线 每一个LineDataSet代表一条线
     * @param mode        折线图是否填充
     */
    private fun initLineDataSet(lineDataSet: LineDataSet, color:Int, mode:Boolean) {
        lineDataSet.color=color
        lineDataSet.setCircleColor(color)
        lineDataSet.lineWidth=2f
        lineDataSet.circleRadius=3f
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(true)
        lineDataSet.valueTextSize=9f

        // 不显示具体值
        lineDataSet.setDrawValues(false)
        //设置折线图填充
        lineDataSet.setDrawFilled(mode);
        //设置填充颜色
        lineDataSet.fillColor=color
        //线模式为圆滑曲线（默认折线）
        lineDataSet.mode=LineDataSet.Mode.LINEAR
    }

    /**
     * 展示线性图(多条)
     *
     * @param xAxisValues
     * @param yAxisValues 多条曲线Y轴数据集合的集合
     * @param color
     */
     fun showLineChart(context: Context, xAxisValues: MutableList<AxisText>, yAxisValues: MutableList<List<Float>>, color: MutableList<Int>) {
        initChart(context)
        var dataSets: ArrayList<ILineDataSet> = ArrayList()
        yAxisValues.forEachIndexed { i, list ->
            var entries = ArrayList<Entry>()//将数据源添加到图标
           list.forEachIndexed { j, fl ->
             entries.add(Entry(xAxisValues[j].code!!,fl))
           }
            var lineDataSet=LineDataSet(entries,"")
            initLineDataSet(lineDataSet,color[i],false)
            dataSets.add(lineDataSet)

        }
        var data = LineData(dataSets)
        var empty = mutableListOf<String>()
        xAxisValues.forEachIndexed { index, it ->
            empty.add(it.name!!)
        }
        xAxis!!.valueFormatter=IndexAxisValueFormatter(empty)
        mlineChart!!.data=data

    }
    /**
     * 设置X轴的值
     *
     * @param max
     * @param min
     * @param labelCount
     */
    fun setXAxis(max: Float, min: Float, labelCount: Int,force:Boolean) {
        xAxis!!.axisMaximum = max
        xAxis!!.axisMinimum = min
        xAxis!!.setLabelCount(labelCount, force)
        mlineChart!!.invalidate()
    }
}